<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>05_nextTick原理</title>
</head>
<body>
</body>
<script>
  let cbs = [];
  let pendings = false
  function flushCallbacks(){
    cbs.forEach(fn => fn())
    pendings = false
  }
  function nextTick(fn) {
    cbs.push(fn)
    if(!pendings) {
      pendings = true
      Promise.resolve().then(flushCallbacks)
    }
  }
  function render() {
    console.log('渲染')
  }
  nextTick(render)
  nextTick(render)
  nextTick(render)
  console.log('更改状态')
  console.log('更改状态')
  console.log('更改状态')
</script>
</html>